package cn.weakup.thread.pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lixj
 * @date 2022/5/6
 * @desc
 */
public class ThreadPoolExecutorTest {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 10,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(8), new ThreadPoolExecutor.CallerRunsPolicy());
        CountDownLatch countDownLatch = new CountDownLatch(16);
        for (int i = 0; i < 16; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                    countDownLatch.countDown();
                }
            });
        }
        System.out.println("当前剩余计数：" + countDownLatch.getCount());

        System.out.println("正在等待线程池中的任务执行完成");
        // 等待线程池终止
        countDownLatch.await();

        executor.shutdown();
        System.out.println("所有线程执行结束，线程池关闭");

    }
}
